package com.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.common.domain.dto.StudentDormQueryDTO;
import com.common.domain.vo.StudentDormPageVO;
import com.common.domain.vo.StudentDormVO;
import com.service.serviceInterface.StudentDormService;
import com.system.entity.Dorm;
import com.system.entity.StudentDorm;
import com.system.entity.StudentInfo;
import com.system.mapper.StudentDormMapper;
import com.system.mapper.StudentInfoMapper;
import com.system.mapper.WorkInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class StudentDormServiceImpl extends ServiceImpl<StudentDormMapper, StudentDorm>
        implements StudentDormService {
    @Resource
    private StudentDormMapper studentDormMapper;
    @Resource
    private WorkInfoMapper workInfoMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Override
    public CommonResult getData(StudentDormQueryDTO dto, long userId) {
        List<Dorm> dorms = workInfoMapper.selectDormIdAndNamesByUserId(userId);
        List<Long> dormIds;
        if (StringUtils.isNotBlank(dto.getDormName())) {
            dormIds = dorms.stream().filter(i -> i.getDormName().contains(dto.getDormName())).map(Dorm::getId).collect(Collectors.toList());
        } else {
            dormIds = dorms.stream().map(Dorm::getId).collect(Collectors.toList());
        }
        if (dormIds.size() == 0) {
            return CommonResult.failed(ResultCode.RETURNNULL);
        }
        List<StudentDorm> studentDorms = studentDormMapper.selectList(new LambdaQueryWrapper<StudentDorm>().in(StudentDorm::getDormId, dormIds));
        List<Long> studentIds = studentDorms.stream().map(StudentDorm::getStudentId).collect(Collectors.toList());
        List<StudentInfo> studentInfos = studentInfoMapper.selectBatchIds(studentIds);
        List<StudentInfo> collect = studentInfos.stream().skip(dto.getPageSize() * (dto.getPageNum() - 1)).limit(dto.getPageSize()).collect(Collectors.toList());
        List<StudentDormVO> vos = new ArrayList<>();
        for (StudentInfo info : collect) {
            StudentDormVO vo = new StudentDormVO();
            BeanUtils.copyProperties(info, vo);
            studentDorms.stream().filter(i -> i.getStudentId().equals(info.getId())).map(StudentDorm::getDormId).forEach(vo::setDormId);
            dorms.stream().filter(j -> j.getId().toString().equals(vo.getDormId())).map(Dorm::getDormName).forEach(vo::setDormName);
            studentDorms.stream().filter(j -> j.getStudentId().equals(info.getId()) && j.getDormId().equals(vo.getDormId())).forEach(j -> {
                vo.setBedId(j.getBedId()).setCheckInTime(j.getCheckinTime().toString()).setAddTime(j.getAddTime()).setAddName(workInfoMapper.selectNameByUserId(j.getAddUserId()));
                if (!Objects.isNull(j.getModifyUserId())) {
                    vo.setModifyTime(j.getModifyTime()).setModifyName(workInfoMapper.selectNameByUserId(j.getModifyUserId()));
                }
            });
            vos.add(vo);
        }
        StudentDormPageVO pageVO = new StudentDormPageVO();
        pageVO.setTotal(studentInfos.size()).setVo(vos);
        return studentInfos.size() > 0 ? CommonResult.success(pageVO) : CommonResult.failed(ResultCode.RETURNNULL);
    }
}




